<?php
#################################
# Metadata Enhancement for Repositories (MER) 
# This software is an overlay repository search interface that uses a 
# text mined index of terms.
# Copyright (C) 2011  Rory McNicholl (University of London Computer Centre), Robert Drinkall (University College London)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Contact repositories@ulcc.ac.uk
# http://www.ulcc.ac.uk

##################################

include("../mer.php");

$mer = new MER();

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
$xml .= "<merlin xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";

if(isset($_REQUEST["termid"]) && is_numeric($_REQUEST["termid"])){
#	$fields = array('se.title', 'record_id', 'term', 'termine_score', 'in_metadata', 'term_id', 'author', 'year', 'source_name', 'material_name', 'full_text', 'inst_image', 'peer_rev');
	$fields = array('record_id', 'term', 'termine_score', 'in_metadata', 'term_id' );

	$termid = $_REQUEST["termid"];
	$condition_string = "where term_id = $termid";
}else{
	$xml .="<response>params not found</response>";
}
if(isset($_REQUEST["full_list"])){
#select distinct record_id  from merlin_associations ma where term_id in (select term_id from merlin_associations where record_id in ('133759')) ;
}
$t_filter = 0;
$t_number = 100;



$max_min_query = "select max(ma.termine_score) as t_max, min(ma.termine_score) as t_min
	from MER_associations ma 
	join MER_terms mt using (term_id) 
	join eprint se on (se.eprintid = ma.record_id) 
	$condition_string ";
	if($t_filter)
		$max_min_query .= " and ma.termine_score > $t_filter";

$max_min_result = @mysql_query($max_min_query) or die("could not complete your query $max_min_query");
$row = @mysql_fetch_array($max_min_result);
$t_w_max = $row["t_max"];
$t_w_min = $row["t_min"];

//	join eprint se on (ma.record_id = se.eprintid)

$tagquery = "select ".join(",",$fields)." 
	from MER_associations ma 
	join MER_terms mt using (term_id) 
	join eprint se on (ma.record_id = se.eprintid)
	$condition_string ";
	if($t_filter)
		$tagquery .= " and ma.termine_score > $t_filter";
	if(!isset($_REQUEST["termid"]))
		$tagquery .=" group by term";
	$tagquery.= " order by -ma.termine_score limit $t_number";

$tagresult = @mysql_query($tagquery) or die("could not complete your query $tagquery");
error_log($tagquery);
header('Content-Type: text/xml');
$xml.= "<tagquery>$tagquery</tagquery>";
$xml .= "<terms>";

$terms = array();
while($row = @mysql_fetch_array($tagresult)) {
	if(!$eprints_server && $row['fileinfo'] != ''){
		$fileinfo = $row['fileinfo'];
		list($thumb, $file) = split(";",$row["fileinfo"]);
		$pattern = '/^(http(s|):\/\/[^\/]+)\/.*$/';
		$eprints_server = preg_replace($pattern,'$1',$file);
	}

	if($_REQUEST["termid"])
		$terms[]=$row;
	else
		$terms[$row["term"]]=$row;
}

$sorted = array_keys($terms);
natcasesort($sorted);
foreach($sorted as $key){
	$row = $terms[$key];
	$term = $row['term'];
	$term_id = $row['term_id'];
	$score = $row['termine_score'];
	$in_meta = $row['in_metadata'];
	$record_id = $row['record_id'];
#	$title = htmlspecialchars($row['title']);

#	$author = $row['author'];
	//more accurate pubdates may be available but this will always be here
	$d = $row['datestamp_day'];
	$m = $row['datestamp_month'];
	$y = $row['datestamp_year'];
	$h = $row['datestamp_hour'];
	$i = $row['datestamp_minute'];
	$s = $row['datestamp_second'];
	$pubDate = date('r',mktime($h,$i,$s,$m,$d,$y));
	
#	$source_name = $row['source_name'];
#	$material_name = $row['material_name'];
	$full_text = $row['full_text_status'];
#	$inst_image = $row['inst_image'];
#	$peer_rev = $row['peer_rev'];

	#here's a wheeze	
	#get (fully rendered) citation data from repo interface with only id
	#could lead to knocking out one of the joins (the one to eprint table)

	$citation_url = $eprints_server."/cgi/export/$record_id/HTML/{$mer->config->repo_id}-eprint-$record_id.html";
	$citation_html = file_get_contents($citation_url);

#		<title>$title</title>

	$term_element = <<<EOT
	<term>
		<termid>$term_id</termid>
		<text>$term</text>
		<score>$score</score>
		<in_meta>$in_meta</in_meta>
		<record_id>$record_id</record_id>
		<author>$author</author>
		<pubDate>$pubDate</pubDate>
		<citation>$citation_html</citation>
		<source>$source_name</source>
		<material>$material_name</material>
		<full_text>$full_text</full_text>
		<source_logo>$inst_image</source_logo>
		<peer_review>$peer_rev</peer_review>
	</term>
EOT;

	$xml .= $term_element;
}
$xml .= "</terms></merlin>";

print $xml;
?>
